#include "../src/thread/thread.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// ABDH#K###E##CFI###G#J##
// ABD#G##E##CF###
int main(int argc, const char *argv[])
{
    int ret = 0;
    BiTNode *tree = NULL;

    // 创建一个二叉树
    printf("请按照先序输入二叉树(空节点用#表示)：");
    tree = fBiTree.create();

    if (tree != NULL)
    {
        printf("\n二叉树创建成功！\n");
    }
    else
    {
        printf("\n二叉树创建出现异常！\n");
        ret = -1;
        goto ERROR_END;
    }

    printf("先序遍历输出：");
    fBiTree.prevOrder(tree);
    printf("\n");
    printf("中序遍历输出：");
    fBiTree.inOrder(tree);
    printf("\n");
    printf("后序遍历输出：");
    fBiTree.postOrder(tree);
    printf("\n");
    printf("层序遍历输出：");
    fBiTree.levelOrder(tree);
    printf("\n\n");

    int leaf = 0;
    fBiTree.leafNum(tree, &leaf);
    printf("节点的个数 = %d  叶子节点的个数 = %d, 树的深度/高度 = %d\n\n",
           fBiTree.nodeNum(tree), leaf, fBiTree.depth(tree));

    BiTNode *copyTree = fBiTree.copy(tree);

    BiTNode *tmp = NULL;
    fthread.prevThread(copyTree, &tmp);

    printf("copy树先序线索化正序遍历输出：");
    fthread.Older(copyTree);

    printf("\ncopy树先序线索化正序遍历输出：");
    fthread.byNextOlder(copyTree);

    // printf("copy树先序线索化倒序遍历输出：");
    // fthread.byPrevOlder(copyTree);
    printf("\n");

    tmp = NULL;

ERROR_END:
    // 释放二叉树
    fBiTree.release(tree);
    /* copyTree已经在线索化的过程中别修改，此时按照原先的释放方式释放的话出现异常 */
    // fBiTree.release(copyTree);

    printf("system exited with return code %d\n", ret);

    return ret;
}
/*
ABDH#K###E##CFI###G#J##
*/
